#!/bin/sh

# Run from this directory
cd "${0%/*}" || exit 1

# Source tutorial run functions
. "$WM_PROJECT_DIR/bin/tools/RunFunctions"

usage () {
    exec 1>&2
    while [ "$#" -ge 1 ]; do printf "\n%s\n" "$1"; shift; done
    cat <<USAGE

Usage: ${0##*/} [OPTIONS]
options:
  -h | -help             help

Executes the following
+ 'Allmesh' to generate the mesh
+ 'foamRun' to run the simulation
+ 'createGraphs' to create graphs

Options can be applied on the command line which are passed to Allmesh
and processed as follows:
USAGE
    ./Allmesh -h
    exit 1
}

# Calculation function
calc () {
    # Avoid locale problems with output of numbers from awk
    export LC_ALL=C

    awk "BEGIN{printf(\"%.2f\", $1)}"
}

meshArgs=""
while [ "$#" -gt 0 ]
do
    case "$1" in
    -b | -bafflesOff | -g | -graded | -r | -refineRotating)
        meshArgs="$meshArgs $1"
        shift 1
        ;;
    -i | -impeller | -s | -size)
        [ "$#" -ge 2 ] || usage "'$1' option requires an argument"
        meshArgs="$meshArgs $1 $2"
        shift 2
        ;;
    -h | -help)
        usage
        ;;
    -test)
        shift 1
        ;;
    -*)
        usage "Invalid option '$1'"
        ;;
    *)
        break
        ;;
    esac
done
[ "$#" -eq 0 ] || usage "Arguments specified ($#) =/= required (0)"

#shellcheck disable=SC2086
[ "$meshArgs" ] && set -- "$@" $meshArgs

# Generate the mesh if not present
[ -d "constant/polyMesh" ] || ./Allmesh "$@"

runApplication foamRun

PbyRho="$(awk 'END{print $2}' postProcessing/power/0/power.dat)"
D="$(awk -F'[; ]+' '/^D/ {print $2}' system/blockMeshDict.orig)"
rpm="$(awk '/^omega/ {print $2}' constant/rotatingZoneProperties)"

echo "Power number = $(calc "$PbyRho/(1e9*($rpm/60)^3*(0.3*$D)^5)")"

#------------------------------------------------------------------------------
